package app.netant.bt;

import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

/**
 * 磁链基础实体
 * @author JiaJ
 *
 */
public class MargnetEntity {
	
	private String margnetID = System.currentTimeMillis() + MathUtil.random(3);
	private String srcURL = "";
	private String margnetURL = "";
	private String margnetName = "";
	private String margnetNameSim = "";
	private String margnetType = "";
	private String margnetDate = "";
	private String margnetFile = "";
	private String margnetSize = "";
	
	public String getMargnetID() {
		return margnetID;
	}
	public void setMargnetID(String margnetID) {
		if(margnetID == null){
			this.margnetID = "";
			return;
		}
		this.margnetID = margnetID;
	}
	public String getSrcURL() {
		return srcURL;
	}
	public void setSrcURL(String srcURL) {
		if(srcURL == null){
			this.srcURL = "";
			return;
		}
		this.srcURL = srcURL;
	}
	public String getMargnetURL() {
		return margnetURL;
	}
	public void setMargnetURL(String margnetURL) {
		if(margnetURL == null){
			this.margnetURL = "";
			return;
		}
		this.margnetURL = margnetURL;
	}
	public String getMargnetName() {
		return margnetName;
	}
	public void setMargnetName(String margnetName) {
		if(margnetName == null){
			this.margnetName = "";
			return;
		}
		this.margnetName = this.getString(margnetName.trim());
		this.margnetNameSim = this.getString(margnetName.trim());
		this.margnetNameSim = this.cutString(this.margnetNameSim.trim(), '(', ')');
		this.margnetNameSim = this.cutString(this.margnetNameSim.trim(), '[', ']');
			
		int lastPoint = this.margnetNameSim.lastIndexOf(".");
		if(lastPoint>0){
			this.margnetNameSim = this.margnetNameSim.substring(0,lastPoint);
		}
		
		if(margnetNameSim.endsWith("版")){
			int lastBlank = margnetNameSim.lastIndexOf(" ");
			if(lastBlank>0){
				margnetNameSim = margnetNameSim.substring(0,lastBlank);
			}		
		}
		
		this.margnetNameSim = this.margnetNameSim.trim();
	}
	public String getMargnetNameSim() {
		return margnetNameSim;
	}
	public void setMargnetNameSim(String margnetNameSim) {
		if(margnetNameSim == null){
			this.margnetNameSim = "";
			return;
		}
		this.margnetNameSim = margnetNameSim;
	}
	public String getMargnetType() {
		return margnetType;
	}
	public void setMargnetType(String margnetType) {
		if(margnetType == null){
			this.margnetType = "";
			return;
		}
		this.margnetType = margnetType;
	}
	public String getMargnetDate() {
		return margnetDate;
	}
	public void setMargnetDate(String margnetDate) {
		if(margnetDate == null){
			this.margnetDate = "";
			return;
		}
		this.margnetDate = margnetDate;
	}
	public String getMargnetFile() {
		return margnetFile;
	}
	public void setMargnetFile(String margnetFile) {
		if(margnetFile == null){
			this.margnetFile = "";
			return;
		}
		this.margnetFile = this.getString(margnetFile);
		if(this.margnetFile.length()>1300){
			this.margnetFile = this.margnetFile.substring(0,1300)+"...";
		}
	}
	public String getMargnetSize() {
		return margnetSize;
	}
	public void setMargnetSize(String margnetSize) {		
		if(margnetSize == null){
			this.margnetSize = "";
			return;
		}
		this.margnetSize = margnetSize;
	}
	
	public void insert(){
		
		try {
			
			if(this.margnetURL == null || "".equals(this.margnetURL)){
				return;
			}
			
			if(this.margnetName == null || "".equals(this.margnetName)){
				return;
			}
			
			//已经解析过
			DataTable srcTable = DBFactory.getThreadDB().executeQueryTable("select * from ${schema}.NET_MARGNET_SRC where SRC_URL='"+this.srcURL+"'");
			if(srcTable.next()){
				return;
			}
			
			String tolSQL = "insert into ${schema}.NET_MARGNET_TOL "
					             + "(MARGNET_ID,MARGNET_URL,MARGNET_NAME,MARGNET_NAME_SIM,MARGNET_TYP,MARGNET_DATE,MARGNET_FILE,MARGNET_SIZE) "
					             + "values "
					             + "('"+this.margnetID+"','"+this.margnetURL+"','"+this.margnetName+"','"+this.margnetNameSim+"','"+this.margnetType+"','"+this.margnetDate+"','"+this.margnetFile+"','"+this.margnetSize+"') ";
			String urlSQL = "insert into ${schema}.NET_MARGNET_SRC "
					             + "(SRC_ID,MARGNET_ID,SRC_URL) "
					             + "values "
					             + "('"+System.currentTimeMillis() + MathUtil.random(3)+"','"+this.margnetID+"','"+this.srcURL+"') ";
			
			//磁链是否存在
			DataTable margnetTable = DBFactory.getThreadDB().executeQueryTable("select * from ${schema}.NET_MARGNET_TOL where MARGNET_URL='"+this.margnetURL+"'");
			if(margnetTable.next()){
				DBFactory.getThreadDB().executeUpdate(urlSQL);
			}
			
			//不存在
			else{
				DBFactory.getThreadDB().executeUpdate(urlSQL);
				DBFactory.getThreadDB().executeUpdate(tolSQL);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			DBFactory.getThreadDB().executeUpdate("insert into ${schema}.NET_MARGNET_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
					+ "values ('"+System.currentTimeMillis()+MathUtil.random(3)+"','"+this.srcURL+"','2')");
		}
		
	}
	
	protected String cutString(String name,char begin,char end){
		
		StringBuffer sb = new StringBuffer();
		boolean flag = true;
		for(int i=0;i<name.length();i++){
			char active = name.charAt(i);
			
			if(active == begin){
				flag = false;
			}else if(active == end){
				flag = true;
			}else{
				if(flag){
					sb.append(active);
				}
			}
			
		}
		
		return sb.toString();
		
	}
	
	protected String getString(String src){
		return src.replaceAll("'", "’")
				  .replaceAll("\"", "”")
				  .replaceAll("&nbsp;", "")
				  .replaceAll("<br>", "")
				  .replaceAll("<br/>", "")
				  .replaceAll("<p>", "")
				  .replaceAll("</p>", "");
	}
	
	public static void main(String[] args) {
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		DataTable dataTable = DBFactory.getThreadDB().executeQueryTable("select * from ${schema}.NET_MARGNET_TOL");
		while(dataTable.next()){
			
			Enviroment.getEnv().getLogBean().beginLog();
			String id = dataTable.getString("MARGNET_ID");
			String allName = dataTable.getString("MARGNET_NAME");
			String simName = allName;
			
			simName = cutStr(simName.trim(), '(', ')');
			simName = cutStr(simName.trim(), '[', ']');
				
			int lastPoint = simName.lastIndexOf(".");
			if(lastPoint>0){
				simName = simName.substring(0,lastPoint);
			}
			
			simName = simName.trim();
			
			if(simName.endsWith("版")){
				int lastBlank = simName.lastIndexOf(" ");
				if(lastBlank>0){
					simName = simName.substring(0,lastBlank);
				}		
			}
			
			simName = simName.trim();
			
			DBFactory.getThreadDB().executeUpdate("update ${schema}.NET_MARGNET_TOL set MARGNET_NAME_SIM='"+simName+"' where MARGNET_ID='"+id+"'");
			DBFactory.getThreadDB().commit();
			
			Enviroment.getEnv().getLogBean().endLog();
		}
	}
	
	public static String cutStr(String name,char begin,char end){
		
		StringBuffer sb = new StringBuffer();
		boolean flag = true;
		for(int i=0;i<name.length();i++){
			char active = name.charAt(i);
			
			if(active == begin){
				flag = false;
			}else if(active == end){
				flag = true;
			}else{
				if(flag){
					sb.append(active);
				}
			}
			
		}
		
		return sb.toString();
		
	}
	
}
